/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Responses.DataRetrievalComponents;

import Beans.PermissionsBean;
import Beans.UserBean;
import Utilities.Constants.UserAccountLevel;
import java.util.ArrayList;

/**
 * This class represents user data, when retrieved from the system.
 *
 * @author Angel Sanadinov
 */
public class UserDataComponent extends DataRetrievalComponent
{
    private UserBean userData; //user data object
    private boolean isUserLoggedIn; //denotes whether the user is currently logged in
    private PermissionsBean[] permissionsData; //user permissions data

    /**
     * Constructs a user data component using the specified data. <br><br>
     *
     * Used when the service retrieves multiple user data objects.
     *
     * @param userData the user data
     */
    public UserDataComponent(UserBean userData)
    {
        this(userData, false, null);
    }

    /**
     * Constructs a user data component using the specified data. <br><br>
     *
     * Used when the service retrieves a single user data object.
     *
     * @param userData the user data
     * @param isUserLoggedIn denotes whether the user is logged in or not
     * @param permissionsData the user's permissions data
     */
    public UserDataComponent(UserBean userData, boolean isUserLoggedIn, ArrayList<PermissionsBean> permissionsData)
    {
        this.userData = userData;
        this.isUserLoggedIn = isUserLoggedIn;

        if(permissionsData != null && !permissionsData.isEmpty())
            this.permissionsData = permissionsData.toArray(new PermissionsBean[permissionsData.size()]);
        else
            this.permissionsData = null;
    }

    /**
     * Retrieves the user data associated with this object.
     *
     * @return the user data
     */
    public UserBean getUserData()
    {
        return userData;
    }

    /**
     * Retrieves the logged-in status of the user.
     *
     * @return <code>true</code> if the user is logged in or  <code>false</code> if not
     */
    public boolean isUserLoggedIn()
    {
        return isUserLoggedIn;
    }

    /**
     * Retrieves the permissions data associated with this object.
     *
     * @return the permissions data
     */
    public PermissionsBean[] getPermissionsData()
    {
        return permissionsData;
    }

    /**
     * Retrieves the account level associated with the user.
     *
     * @return the user account level or <code>null</code> if it is unavailable
     */
    public UserAccountLevel getAccountLevel()
    {
        UserAccountLevel level = null;
        switch(userData.getAccountLevel())
        {
            case 0: level = UserAccountLevel.DISABLED; break;
            case 1: level = UserAccountLevel.NORMAL; break;
            case 2: level = UserAccountLevel.MANAGER; break;
            case 3: level = UserAccountLevel.OWNER; break;
            default: ; break;
        }

        return level;
    }

    /**
     * Checks the validity of the permissions data associated with this object.
     *
     * @return <code>true</code> if the permissions data is valid or <code>false</code>
     *         otherwise
     */
    private boolean isPermissionsDataValid()
    {
        if(permissionsData != null && permissionsData.length > 0)
        {
            for(PermissionsBean permissionsBean : permissionsData)
            {
                if(!permissionsBean.isValid() || permissionsBean.getUserId() != userData.getUserId())
                    return false;
                else
                    ;
            }
        }
        else
            ;

        return true;
    }

    @Override
    public boolean isValid()
    {
       if(userData != null && userData.isValid() && isPermissionsDataValid())
           return true;
       else
           return false;
    }

    @Override
    public String toString()
    {
        return userData + "," + permissionsData;
    }
}
